<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Serialization to BSON</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb.persistence.html">« Persisting Data</a></li>
      <li style="float: right;"><a href="mongodb.persistence.deserialization.html">Deserialization from BSON »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongodb.persistence.html">Persisting Data</a></li>
    <li>Serialization to BSON</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb.persistence.serialization" class="section">
   <h2 class="title">Serialization to BSON</h2>

   <div class="section">
    <h2 class="title">Arrays</h2>

    <p class="para">
     If an array is a <em class="emphasis">packed array</em> — i.e. empty array or
     the keys start at 0 and are sequential without gaps: <em class="emphasis">BSON
     array</em>.
    </p>

    <p class="para">
     If the array is not packed — i.e. having associative (string) keys, the
     keys don&#039;t start at 0, or when there are gaps:: <em class="emphasis">BSON
     object</em>
    </p>

    <p class="para">
     A top-level (root) document, <em class="emphasis">always</em> serializes as a
     BSON document.
    </p>

    <div class="section">
     <h2 class="title">Examples</h2>

     <p class="para">
      These serialize as a BSON array:
     </p>

     <div class="example-contents">
<div class="textcode"><pre class="textcode">[ 8, 5, 2, 3 ] =&gt; [ 8, 5, 2, 3 ]
[ 0 =&gt; 4, 1 =&gt; 9 ] =&gt; [ 4, 9 ]</pre>
</div>
     </div>


     <p class="para">
      These serialize as a BSON document:
     </p>

     <div class="example-contents">
<div class="textcode"><pre class="textcode">[ 0 =&gt; 1, 2 =&gt; 8, 3 =&gt; 12 ] =&gt; { &quot;0&quot; : 1, &quot;2&quot; : 8, &quot;3&quot; : 12 }
[ &quot;foo&quot; =&gt; 42 ] =&gt; { &quot;foo&quot; : 42 }
[ 1 =&gt; 9, 0 =&gt; 10 ] =&gt; { &quot;1&quot; : 9, &quot;0&quot; : 10 }</pre>
</div>
     </div>


     <p class="para">
      Note that the five examples are <em class="emphasis">extracts</em> of a full
      document, and represent only <em class="emphasis">one</em> value inside a
      document.
     </p>

    </div>
   </div>

   <div class="section">
    <h2 class="title">Objects</h2>

     <p class="para">
      If an object is of the <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span> class, serialize
      as a <em class="emphasis">BSON document</em>.
     </p>

     <p class="para">
      If an object is a supported class that implements
      <span class="interfacename"><a href="class.mongodb-bson-type.html" class="interfacename">MongoDB\BSON\Type</a></span>, then use the BSON
      serialization logic for that specific type.
      <span class="interfacename"><a href="class.mongodb-bson-type.html" class="interfacename">MongoDB\BSON\Type</a></span> instances (excluding
      <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span> may only be
      serialized as a document field value. Attempting to serialize such an
      object as a root document will throw a
      <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
     </p>

     <p class="para">
      If an object is of an unknown class implementing the
      <span class="interfacename"><a href="class.mongodb-bson-type.html" class="interfacename">MongoDB\BSON\Type</a></span> interface, then throw a
      <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
     </p>

     <p class="para">
      If an object is of any other class, without implementing any special
      interface, serialize as a <em class="emphasis">BSON document</em>. Keep only
      <em class="emphasis">public</em> properties, and ignore
      <em class="emphasis">protected</em> and <em class="emphasis">private</em>
      properties.
     </p>

     <p class="para">
      If an object is of a class that implements the
      <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span> interface, call
      <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span> and use
      the returned array or <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span> to serialize as a
      BSON document or array. The BSON type will be determined by the following:
     </p>

     <p class="para">
      <ol type="1">
       <li class="listitem">
        <p class="para">Root documents must be serialized as a BSON
        document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         <span class="interfacename"><a href="class.mongodb-bson-persistable.html" class="interfacename">MongoDB\BSON\Persistable</a></span> objects must be
         serialized as a BSON document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         returns a packed array, serialize as a BSON array.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         returns a non-packed array or <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span>,
         serialize as a BSON document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         did not return an array or <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span>, throw an
         <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
         exception.
        </p>
       </li>
      </ol>
     </p>

     <p class="para">
      If an object is of a class that implements the
      <span class="interfacename"><a href="class.mongodb-bson-persistable.html" class="interfacename">MongoDB\BSON\Persistable</a></span> interface (which
      implies <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span>), obtain
      the properties in a similar way as in the previous paragraphs, but
      <em class="emphasis">also</em> add an additional property
      <span class="property">__pclass</span> as a Binary value, with subtype
      <code class="literal">0x80</code> and data bearing the fully qualified class name
      of the object that is being serialized.
     </p>

     <p class="para">
      The <span class="property">__pclass</span> property is added to the array or
      object returned by
      <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>, which
      means it will overwrite any <span class="property">__pclass</span> key/property in
      the <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.html" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
      return value. If you want to avoid this behaviour and set your own
      <span class="property">__pclass</span> value, you must <em class="emphasis">not</em>
      implement <span class="interfacename"><a href="class.mongodb-bson-persistable.html" class="interfacename">MongoDB\BSON\Persistable</a></span> and
      should instead implement
      <span class="interfacename"><a href="class.mongodb-bson-serializable.html" class="interfacename">MongoDB\BSON\Serializable</a></span> directly.
     </p>

     <div class="section">
      <h2 class="title">Examples</h2>

      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">stdClass&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"foo"&nbsp;:&nbsp;42&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$prot&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"wine"</span><span style="color: #007700">;<br />&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$fpr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"cheese"</span><span style="color: #007700">;<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"foo"&nbsp;:&nbsp;42&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">AnotherClass1&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$prot&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"wine"</span><span style="color: #007700">;<br />&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$fpr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"cheese"</span><span style="color: #007700">;<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'prot'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prot&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"foo"&nbsp;:&nbsp;42,&nbsp;"prot"&nbsp;:&nbsp;"wine"&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">AnotherClass2&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">self&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;MongoDB\Driver\Exception\UnexpectedValueException("bsonSerialize()&nbsp;did&nbsp;not&nbsp;return&nbsp;an&nbsp;array&nbsp;or&nbsp;stdClass")<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">AnotherClass3&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$elements&nbsp;</span><span style="color: #007700">=&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"0"&nbsp;:&nbsp;"foo",&nbsp;"1"&nbsp;:&nbsp;"bar"&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ContainerClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$things&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">AnotherClass4&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$elements&nbsp;</span><span style="color: #007700">=&nbsp;[&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #DD0000">'things'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"things"&nbsp;:&nbsp;{&nbsp;"0"&nbsp;:&nbsp;"foo",&nbsp;"2"&nbsp;:&nbsp;"bar"&nbsp;}&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ContainerClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$things&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">AnotherClass5&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$elements&nbsp;</span><span style="color: #007700">=&nbsp;[&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #DD0000">'things'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"things"&nbsp;:&nbsp;[&nbsp;"foo",&nbsp;"bar"&nbsp;]&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ContainerClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$things&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">AnotherClass6&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Serializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$elements&nbsp;</span><span style="color: #007700">=&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(object)&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #DD0000">'things'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"things"&nbsp;:&nbsp;{&nbsp;"0"&nbsp;:&nbsp;"foo",&nbsp;"1"&nbsp;:&nbsp;"bar"&nbsp;}&nbsp;}<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">UpperClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">Persistable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$prot&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"wine"</span><span style="color: #007700">;<br />&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$fpr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"cheese"</span><span style="color: #007700">;<br />&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">():&nbsp;array&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'prot'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prot&nbsp;</span><span style="color: #007700">];<br />&nbsp;&nbsp;}<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;=&gt;&nbsp;{&nbsp;"foo"&nbsp;:&nbsp;42,&nbsp;"prot"&nbsp;:&nbsp;"wine",&nbsp;"__pclass"&nbsp;:&nbsp;{&nbsp;"$type"&nbsp;:&nbsp;"80",&nbsp;"$binary"&nbsp;:&nbsp;"VXBwZXJDbGFzcw=="&nbsp;}&nbsp;}</span>
</span>
</code></div>
     </div>

    </div>
   </div>
  </div></div></div></body></html>